{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "B\n",
      "C\n"
     ]
    }
   ],
   "source": [
    "# 3.1\n",
    "names = [\"A\", \"B\", \"C\"]\n",
    "for i in names:\n",
    "    print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, A\n",
      "Hello, B\n",
      "Hello, C\n"
     ]
    }
   ],
   "source": [
    "# 3.2\n",
    "names = [\"A\", \"B\", \"C\"]\n",
    "for i in names:\n",
    "    print(f\"Hello, {i}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I would like to own a bike\n",
      "I would like to own a car\n",
      "I would like to own a bus\n"
     ]
    }
   ],
   "source": [
    "# 3.3\n",
    "traffic_way = [\"bike\", \"car\", \"bus\"]\n",
    "for i in traffic_way:\n",
    "    print(f\"I would like to own a {i}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "invite A\n",
      "invite B\n",
      "invite C\n"
     ]
    }
   ],
   "source": [
    "# 3.4\n",
    "name_list = [\"A\", \"B\", \"C\"]\n",
    "for name in name_list:\n",
    "    print(f\"invite {name}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C is absent.\n",
      "invite A\n",
      "invite B\n",
      "invite D\n"
     ]
    }
   ],
   "source": [
    "# 3.5\n",
    "name_list = [\"A\", \"B\", \"C\"]\n",
    "print(f\"{name_list[2]} is absent.\")\n",
    "name_list[2] = \"D\"\n",
    "for name in name_list:\n",
    "    print(f\"invite {name}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "invite F\n",
      "invite A\n",
      "invite G\n",
      "invite B\n",
      "invite C\n",
      "invite D\n",
      "invite E\n",
      "invite H\n"
     ]
    }
   ],
   "source": [
    "# 3.6\n",
    "name_list = [\"A\", \"B\", \"C\", \"D\", \"E\"]\n",
    "name_list.insert(0, \"F\")\n",
    "name_list.insert(2, \"G\")\n",
    "name_list.append(\"H\")\n",
    "for name in name_list:\n",
    "    print(f\"invite {name}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "invite F\n",
      "invite A\n",
      "invite G\n",
      "invite B\n",
      "invite C\n",
      "invite D\n",
      "invite E\n",
      "invite H\n",
      "But I can only invite two of them\n",
      "----------------------------------\n",
      "H, I'm sorry. I can't invite you.\n",
      "E, I'm sorry. I can't invite you.\n",
      "D, I'm sorry. I can't invite you.\n",
      "C, I'm sorry. I can't invite you.\n",
      "B, I'm sorry. I can't invite you.\n",
      "G, I'm sorry. I can't invite you.\n",
      "F, I still invite you.\n",
      "A, I still invite you.\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# 3.7\n",
    "name_list = [\"A\", \"B\", \"C\", \"D\", \"E\"]\n",
    "name_list.insert(0, \"F\")\n",
    "name_list.insert(2, \"G\")\n",
    "name_list.append(\"H\")\n",
    "for name in name_list:\n",
    "    print(f\"invite {name}\")\n",
    "print(\"But I can only invite two of them\")\n",
    "\n",
    "print(\"----------------------------------\")\n",
    "\n",
    "while (len(name_list) > 2):\n",
    "    name = name_list.pop()\n",
    "    print(f\"{name}, I'm sorry. I can't invite you.\")\n",
    "while name_list:\n",
    "    name = name_list[0]\n",
    "    print(f\"{name}, I still invite you.\")\n",
    "    del name_list[0]\n",
    "    \n",
    "print(name_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['A', 'D', 'E', 'B', 'C']\n",
      "\n",
      "\n",
      "['A', 'B', 'C', 'D', 'E']\n",
      "['A', 'D', 'E', 'B', 'C']\n",
      "['E', 'D', 'C', 'B', 'A']\n",
      "['A', 'D', 'E', 'B', 'C']\n",
      "\n",
      "\n",
      "['C', 'B', 'E', 'D', 'A']\n",
      "['A', 'D', 'E', 'B', 'C']\n",
      "\n",
      "\n",
      "['A', 'B', 'C', 'D', 'E']\n",
      "['E', 'D', 'C', 'B', 'A']\n"
     ]
    }
   ],
   "source": [
    "# 3.8\n",
    "places = [\"A\", \"D\", \"E\", \"B\", \"C\"]\n",
    "\n",
    "print(places)\n",
    "print('\\n')\n",
    "\n",
    "#sorted 仅返回排序后的列表，不改变原有顺序\n",
    "print(sorted(places))\n",
    "print(places)\n",
    "\n",
    "print(sorted(places, reverse = True))\n",
    "print(places)\n",
    "\n",
    "print('\\n')\n",
    "#reserve 将列表中元素排列顺序倒转\n",
    "places.reverse()\n",
    "print(places)\n",
    "\n",
    "places.reverse()\n",
    "print(places)\n",
    "\n",
    "print('\\n')\n",
    "#sort 将列表中元素顺序排列，无返回值\n",
    "places.sort()\n",
    "print(places)\n",
    "\n",
    "places.sort(reverse = True)\n",
    "print(places)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "invite F\n",
      "invite A\n",
      "invite G\n",
      "invite B\n",
      "invite C\n",
      "invite D\n",
      "invite E\n",
      "invite H\n",
      "But I can only invite two of them\n",
      "----------------------------------\n",
      "H, I'm sorry. I can't invite you.\n",
      "E, I'm sorry. I can't invite you.\n",
      "D, I'm sorry. I can't invite you.\n",
      "C, I'm sorry. I can't invite you.\n",
      "B, I'm sorry. I can't invite you.\n",
      "G, I'm sorry. I can't invite you.\n",
      "F, I still invite you.\n",
      "A, I still invite you.\n",
      "[]\n",
      "The number of guests is 2\n"
     ]
    }
   ],
   "source": [
    "# 3.9\n",
    "name_list = [\"A\", \"B\", \"C\", \"D\", \"E\"]\n",
    "name_list.insert(0, \"F\")\n",
    "name_list.insert(2, \"G\")\n",
    "name_list.append(\"H\")\n",
    "for name in name_list:\n",
    "    print(f\"invite {name}\")\n",
    "print(\"But I can only invite two of them\")\n",
    "\n",
    "print(\"----------------------------------\")\n",
    "\n",
    "while (len(name_list) > 2):\n",
    "    name = name_list.pop()\n",
    "    print(f\"{name}, I'm sorry. I can't invite you.\")\n",
    "num = len(name_list)\n",
    "while name_list:\n",
    "    name = name_list[0]\n",
    "    print(f\"{name}, I still invite you.\")\n",
    "    del name_list[0]\n",
    "\n",
    "    \n",
    "print(name_list)\n",
    "print(f\"The number of guests is {num}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#3.10 略\n",
    "#3.11 有意引发错误\n",
    "# lst = [\"0\", \"1\"]\n",
    "# print(lst[2])\n",
    "#IndexError: list index out of range\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
